using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Dapr;
using EasyDapr.Domain;
using EasyDapr.Infrastructure;
using EasyDapr.Infrastructure.App.Dtos;
using EasyDapr.Infrastructure.App.Requests.Identity;
using EasyDapr.Infrastructure.Attribute;
using EasyDapr.Infrastructure.Response;
using Microsoft.AspNetCore.Mvc;

namespace IdentityService.Controllers
{
    [ApiController]
    public class UserDaprController : ControllerBase
    {
        [ApiIgnoreFilter]
        [HttpPost("/get-user-by-id")]
        public async Task<ApiResult<UserDto>> GetById(UserByIdRequest requestData)
        {
            var rsp = new ApiResult<UserDto>();
            var result = GetUsers().FirstOrDefault(x => x.Id == requestData.UserId);
            if (result == null)
            {
                Console.WriteLine("找不到数据 ");
                // throw new CoreException("找不到数据");
                rsp.Message = "找不到数据";
                return rsp;
            }

            Console.WriteLine(JsonSerializer.Serialize(requestData));
            rsp.Success = true;
            rsp.Data = result;
            return await Task.FromResult(rsp);
        }

        [Topic(Consts.PubSubName, "topic_test")]
        [HttpPost("/topic_test")]
        public async Task<ActionResult<UserDto>> TopicTest(UserByIdRequest requestData)
        {
            var result = GetUsers().FirstOrDefault(x => x.Id == requestData.UserId);
            Console.WriteLine(JsonSerializer.Serialize(result));
            return await Task.FromResult(result);
        }

        [Topic(Consts.PubSubName, "InventoryCreated")]
        [HttpPost("/topic_test1")]
        public async Task<ActionResult> InventoryCreated(dynamic requestData)
        {
            Console.WriteLine("---------------");
            Console.WriteLine(JsonSerializer.Serialize(requestData));
            return await Task.FromResult(Ok());
        }

        private static IEnumerable<UserDto> GetUsers()
        {
            return new List<UserDto>
            {
                new UserDto
                {
                    Id = "88421113",
                    FullName = "Bob Smith",
                    Email = "BobSmith@email.com",
                    Address = "One Hacker Way, Heidelberg, 69118, Germany"
                },
                new UserDto
                {
                    Id = "818727",
                    FullName = "Alice Smith",
                    Email = "AliceSmith@email.com",
                    Address = "One Hacker Way, Heidelberg, 69118, Germany"
                }
            };
        }
    }
}